var map = null;
var geocoder;
var base_url ;//= "http://localhost/cellplan/";
var site_url ;//= "http://localhost/cellplan/index.php/";

var polyPathdesa=[];
var polyPathkec=[];

var polyHoverDes=[];

google.maps.event.addDomListener(window, 'load', initialize);
var infoWindow ;

//var markers_tower = [];


function initialize() {
  base_url = document.getElementById("base_url").value;
  site_url = document.getElementById("site_url").value;
    
  infoWindow = new google.maps.InfoWindow();

    
  var myOptions = {
    zoom: 11,
    center: new google.maps.LatLng(-8.219021,114.369105),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    zoomControl: true,
    zoomControlOptions: {
        style: google.maps.ZoomControlStyle.LARGE,
        position: google.maps.ControlPosition.LEFT_BOTTOM
    },
    mapTypeControl: true,
    mmapTypeControlOptions: {
        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
        position: google.maps.ControlPosition.CENTER_BOTTOM
    },
    panControl: true,
    panControlOptions: {
        position: google.maps.ControlPosition.LEFT_BOTTOM
    },
   streetViewControl: false

  }
  map = new google.maps.Map(document.getElementById("map_canvas"),  myOptions);
  
  draw_poly_kec();
  draw_poly_desa();
  
    $('#kecbox').change(function(){
        if(this.checked){
            clear_layer_all(polyPathkec);
            draw_poly_kec();
        }else{
            clear_layer_all(polyPathkec);
        }
    });
    
    $('#desabox').change(function(){
        if(this.checked){ 
            clear_layer_all(polyPathdesa);
            draw_poly_desa();
        }else{
            clear_layer_all(polyPathdesa);
        }
    });
    
    $('#pilihkecamatan').change(function(){
        
        document.getElementById("id_kec").value = $('#pilihkecamatan option:selected').val();
        var id =$('#pilihkecamatan option:selected').val();
//        clear_kecamatan();
//        clear_desa();
          //clear_layer_all(polyPathdesa);
          //clear_layer_all(polyHoverDes);
          
          polyPathdesa[id].setOptions({strokeWeight: 1, fillColor: 'green'});
          
//        draw_poly_kec();
//        draw_poly_desa();
          //polyHoverDes[id].setMap(map);
    });
    
    

}


function add_polygon_desa(poly_data_lgkp,index){
    var titik_poly=[];
    var point_label ;
    
    
    var poly_data = poly_data_lgkp.DESA_POINT_LIST;
    for(var a=0;a<poly_data.length-1;a++){
        titik_poly[a] = new google.maps.LatLng(poly_data[a].LAT,poly_data[a].LNG); 
    }
    var bounds = new google.maps.LatLngBounds();
    for (i = 0; i < titik_poly.length; i++) {
        bounds.extend(titik_poly[i]);
    }
    
    polyPathdesa[index] = new google.maps.Polygon({
                 paths: titik_poly,
                 strokeColor: "#000000",
                 strokeOpacity: 0.6,
                 strokeWeight: 0.2,
                 fillColor: poly_data_lgkp.COLOR,
                 fillOpacity: 0.2
     });
     
     polyHoverDes[index] = new google.maps.Polygon({
                 paths: titik_poly,
                 strokeColor: "#333333",
                 strokeOpacity: 1,
                 strokeWeight: 0.3,
                 fillColor: "#000000",
                 fillOpacity: 0.7
    });
     
     google.maps.event.addListener(polyPathdesa[index], "mouseover", function(event) {
          
         polyHoverDes[index].setMap(map);
               //map.setCenter(bounds.getCenter());
               //map.fitBounds(bounds);
     });    
     google.maps.event.addListener(polyPathdesa[index], "mouseout", function(event) {
         polyHoverDes[index].setMap(null);        
     });
     google.maps.event.addListener(polyPathdesa[index], "click", function(event) {
         //alert(bounds.getCenter());    
            map.fitBounds(bounds);
         // alert(poly_data_lgkp.DESA_NAMA+",  "+poly_data_lgkp.KECAMATAN_NAMA);
     });  
     polyPathdesa[index].setMap(map);
}
//
//function clear_desa(){
//    if (polyPathdesa) {
//            for ( i in polyPathdesa) {
//                    polyPathdesa[i].setMap(null);
//            }
//        }
//}
//
//function clear_kecamatan(){
//    if (polyPathkec) {
//            for ( i in polyPathkec) {
//                    polyPathkec[i].setMap(null);
//            }
//        }
//}

function clear_layer_all(path){
    if (path) {
            for ( i in path) {
                    path[i].setMap(null);
            }
        }
}

function draw_poly_desa(){
        var id_kec = document.getElementById("id_kec").value;
        $.ajax({
            type: "POST",
            url: site_url+"/map/get_polygon_desa/"+id_kec,
            dataType: 'json',
            success: function(json) {
                    //alert(json.Desa.length);
                    if (json.Desa.length > 0) {
                        for (var i=0; i<json.Desa.length; i++) {
                            var point_des = json.Desa[i];
                            //alert(point_des);
                            add_polygon_desa(point_des,i);
                        }
                    }
            }
        });
    return false;
}

function add_polygon_kec(poly_data_lgkp,index){
    var titik_poly=[];
    var point_label ;
    
    var poly_data = poly_data_lgkp.KEC_POINT_LIST;
    for(var a=0;a<poly_data.length-1;a++){
        titik_poly[a] = new google.maps.LatLng(poly_data[a].LAT,poly_data[a].LNG); 
    }
    var bounds = new google.maps.LatLngBounds();
    for (i = 0; i < titik_poly.length; i++) {
        bounds.extend(titik_poly[i]);
    }
    polyPathkec[index] = new google.maps.Polygon({
                 paths: titik_poly,
                 strokeColor: "#000000",
                 strokeOpacity: 1,
                 strokeWeight: 0.5,
                 fillColor: poly_data_lgkp.COLOR,
                 fillOpacity: 0.1
     });
     
     var poly_add2 = new google.maps.Polygon({
                 paths: titik_poly,
                 strokeColor: "#000000",
                 strokeOpacity: 1,
                 strokeWeight: 0.3,
                 fillColor: "#000000",
                 fillOpacity: 0.7
    });
     
     google.maps.event.addListener(polyPathkec[index], "mouseover", function(event) {
              poly_add2.setMap(map);
     });    
     google.maps.event.addListener(polyPathkec[index], "mouseout", function(event) {
              poly_add2.setMap(null);
     });
     google.maps.event.addListener(polyPathkec[index], "click", function(event) {
         alert(poly_data_lgkp.KEC_NAMA+",  "+poly_data_lgkp.PROV_NAMA); 
         map.fitBounds(bounds);
          poly_add2.setMap(map);
         
     });  
     polyPathkec[index].setMap(map);
}

function draw_poly_kec(){
        var id_kec = document.getElementById("id_kec").value;
        $.ajax({
            type: "POST",
            url: site_url+"/map/get_polygon_kec/"+id_kec,
            dataType: 'json',
            success: function(json) {
                    //alert(json.Desa.length);
                    if (json.Desa.length > 0) {
                        for (var i=0; i<json.Desa.length; i++) {
                            var point_kec = json.Desa[i];
                            //alert(point_des);
                            add_polygon_kec(point_kec,i);
                        }
                    }
            }
        });
    return false;
}